#coding=UTF-8
print("-----get the pic from alibaba begin-----")
#
# 读取网页源文件 返回content文本字符串; 替换转义字符&quot; ~ "
# 正则获取:
#<span class="image" title="淡黄色" data-imgs='{"preview":"https://cbu01.alicdn.com/img/ibank/2019/985/877/10822778589_981562703.400x400.jpg","original":"https://cbu01.alicdn.com/img/ibank/2019/985/877/10822778589_981562703.jpg"}'>
# 正则获取titile、original
# 正则获取：<img alt="undefined" src="https://cbu01.alicdn.com/img/ibank/2019/556/323/10796323655_981562703.jpg" max-width="790" style="visibility: visible; max-width: 790px; zoom: 1;">
# 正则获取src
#
#
from urllib import request
import re,time,requests,threading,imghdr,os
from PIL import Image,ImageDraw,ImageFont
from shutil import copyfile
import cv2 as cv
import numpy as np


#判断图片类型
def whatTypeImg(_img):
    return imghdr.what(_img)

#得到一个patter
def getRePattern(_express):
    return re.compile(_express)

#用patter得到要检索的字符串数组
def getTargetList(_express,_source_str):
    _patter = getRePattern(_express)
    return _patter.findall(_source_str)

#读取文本文件 返回html content字符串
def getHtmlContent(_file):
    _file_stream = open(_file,'r', encoding='utf-8')
    _content = _file_stream.read()
    _file_stream.close()
    return _content

#截取url中的扩展名
def getSuffixFromUrl(_url):
    _divided = _url.split('.')
    _append_front = r'.'
    if '?' in _divided[-1]:
        _temp_array = _divided[-1].split('?')
        return _append_front + _temp_array[0]
    return _append_front +_divided[-1]

#截取url中的文件名
def getFileNameFromUrl(_url):
    _divided = _url.split('/')
    return _divided[-1]

#生成文档
def genArchive(_file_name,_content):
    _file = open(_file_name,'wb')
    _file.write(_content)
    _file.close()

#启动线程
def startThread(_url,_fileName):
    t = threading.Thread(target=downloadArchive,args=(_url,_fileName))
    t.setDaemon(True)
    t.start()
    t.join()

#下载文档
PATH_ROOT = './taobaopics/sourcepics/'
TARGET_PIC_ROOT = './taobaopics/targetpic/'
WATERMARK_PIC_ROOT = './taobaopics/watermarkpic/'
def downloadArchive(_url,_local_file_name):
    if requests.head(_url).status_code not in (200,403):
        return
    _response = request.urlopen(_url)
    _content = _response.read()
    if not os.path.exists(PATH_ROOT):
        os.makedirs(PATH_ROOT)
    _file_img = PATH_ROOT+_local_file_name
    _file = open(_file_img,'wb')
    _file.write(_content)
    _file.close()
    print('downloadArchive : %s' % _file_img)

#(.+)?和(.+?)区别???
#OPTION_HTML_PATTERN = '<span class="image.*?" title="(.+)?" data-imgs="{"preview":"(.+)?","original":"(.+)?"}">'
#OPTION_HTML_PATTERN_1 = '<span class="image.*?" title="(.+?)" data-imgs="{"preview":"(.+?)","original":"(.+?)"}">'
#OPTION_HTML_PATTERN_2 = '<div class="unit-detail-spec-operator.*?" data-unit-config="{"name":"(.+?)"}" data-imgs="{"preview":"(.+?)","original":"(.+?)"}">'
OPTION_JSON_PATTERN = '<div class="sku-item-image" style="background: url\("(.+?)"\)(.+?)"></div><div class="sku-item-left"><div class="sku-item-name">(.+?)</div>'
THUMBNAIL_IMAGE_HTML_PATTERN_1 = '<li class=".*?" data-imgs="{"preview":"(.+?)","original":"(.+?)"}">'
#DETAIL_HTML_PATTERN = '<img alt="undefined" src="(.+)?" max-width="790" style="(.+)?"><br>'
#DETAIL_HTML_PATTERN = ' alt="(.+?)" .*?src="(.+?)"'
#DETAIL_HTML_PATTERN_1 = '<img align="(.+?)" src="(.+?)"'
DETAIL_HTML_PATTERN_2 = '<img(.+?)src="(.+?)"'
#SHOP_NAME_PATTERN = '<a href=\"#\" class=\"company-name\">(.+?)</a>'
SHOP_NAME_PATTERN = '<span title=\"(.+?)\"'
#判断图片大小是否符合需求
def isNeeded(_img):
    k,h = Image.open(_img).size
    if k<600 and h<600:
        return False
    return True
#添加水印
def addWatermark(from_img,txt,to_img):
    img = Image.open(from_img).convert('RGBA')
    mark = Image.new('RGBA',img.size,(0,0,0,0))
    fnt = ImageFont.truetype('./simsun.ttc',30)
    draw = ImageDraw.Draw(mark)
    draw.text((10,img.height-35),txt,font=fnt,fill=(0,0,0,70))
    out = Image.alpha_composite(img,mark)
    rev_to_img = to_img[::-1]
    dotIndex = rev_to_img.index('.')
    toImgLen = len(to_img)
    to_img = to_img[:toImgLen-dotIndex]+'png'
    out.save(to_img,quality=50)

def addWatermarkOne(from_img,txt,to_img):
    # 当前目录读取一张图片
    img = cv.imread(from_img,cv.IMREAD_UNCHANGED)
    # 尺寸
    (height,width,xx) = img.shape
    #print((height,width,xx))
    # 图片转换（cv2 -> pil）
    cv2img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
    pilimg = Image.fromarray(cv2img)
    # 在图片上添加文字（支持中文）
    draw = ImageDraw.Draw(pilimg)
    font = ImageFont.truetype("simhei.ttf", 30, encoding="utf-8")
    draw.text((10, height-35), txt,(0,0,0), font=font)
    # 图片转换（pil -> cv2）
    cv2img2 = cv.cvtColor(np.array(pilimg), cv.COLOR_RGB2BGR)
    # 保存图片到当前目录
    cv.imwrite(to_img, cv2img2)
    # 显示图片
    cv.imshow("show Chloe2", cv2img2)
    cv.waitKey(0)

#遍历图片 删除不需要的图片 留下的加水印
def processPic(_watermark):
    for pic in os.listdir(PATH_ROOT):
        if not os.path.exists(TARGET_PIC_ROOT):
            os.makedirs(TARGET_PIC_ROOT)
        if not os.path.exists(WATERMARK_PIC_ROOT):
            os.makedirs(WATERMARK_PIC_ROOT)
        if not isNeeded(PATH_ROOT+pic):
            continue
        replaceSuffix(pic)
    #for pic in os.listdir(TARGET_PIC_ROOT):
    #    addWatermark(TARGET_PIC_ROOT+pic,_watermark,WATERMARK_PIC_ROOT+pic)
    #    addWatermarkOne(TARGET_PIC_ROOT+pic,_watermark,WATERMARK_PIC_ROOT+pic)

#判断图片类型是否与扩展名匹配 不匹配则修改扩展名
def replaceSuffix(_img):
    _img_temp = _img.split('.')
    if len(_img_temp) > 1 :
        _target_img = _img
        sourceImg = PATH_ROOT+_img
        targImg = TARGET_PIC_ROOT+_target_img
        _source_suffix = _img_temp[1]
        _current_img_type = whatTypeImg(sourceImg)
        if _source_suffix != _current_img_type and _current_img_type != None:
            targImg = targImg.replace('.'+_source_suffix,'.'+_current_img_type)
            copyfile(sourceImg,targImg)
            #os.remove(sourceImg)
        if whatTypeImg(targImg) == 'gif':
            img = Image.open(targImg).convert('RGB')
            img.save(targImg.replace('.gif','.jpeg'),'JPEG')
            os.remove(targImg)

#给序号拼’0’ 方便排序
BIT_SUM = 2 #序号位数
def penddingIndex(_index):
    strIndex = str(_index)
    strIndexLen = len(strIndex)
    if len(strIndex) < BIT_SUM :
        return '0'*(BIT_SUM - strIndexLen) + strIndex
    return strIndex
if __name__ == '__main__':
    #processPic('图片源自')
    #exit()
    try:
        content = getHtmlContent(r'./albbhtml.html').replace('&quot;','"').replace('\\','')
        #获取店铺名称
        shopNameList = getTargetList(SHOP_NAME_PATTERN,content)
        print(shopNameList)
        #下载分类选项处图片
        #optionTargetList = getTargetList(OPTION_HTML_PATTERN_1,content)
        #optionTargetList.extend(getTargetList(OPTION_HTML_PATTERN_2,content))
        optionTargetList = getTargetList(OPTION_JSON_PATTERN,content)
        _current_url = ''
        for target in optionTargetList:
            _current_url = target[0]
            #downloadArchive(_current_url,target[0]+getSuffixFromUrl(_current_url))
            ####### thread begin #############
            startThread(_current_url,target[2]+getSuffixFromUrl(_current_url))
            ####### thread end #############
        #下载缩略图
        thumbnailImagesList = getTargetList(THUMBNAIL_IMAGE_HTML_PATTERN_1,content)
        index = 0
        for image in thumbnailImagesList:
            _current_url = image[1]
            #downloadArchive(_current_url,time.strftime("%Y-%m-%d ",time.localtime())+'image '+penddingIndex(index)+getSuffixFromUrl(_current_url))
            ####### thread begin #############
            startThread(_current_url,time.strftime("%Y-%m-%d ",time.localtime())+'image '+penddingIndex(index)+getSuffixFromUrl(_current_url))
            ####### thread end #############
            index+=1
        #下载明细图片
        detailTargetList = getTargetList(DETAIL_HTML_PATTERN_2,content)
        #detailTargetList.extend(getTargetList(DETAIL_HTML_PATTERN_1,content))
        #detailTargetList.extend(getTargetList(DETAIL_HTML_PATTERN,content))
        #index = len(detailTargetList)
        index = 0
        for detail in detailTargetList:
            _current_url = detail[1]
            #downloadArchive(detail[1],time.strftime("%Y-%m-%d %H%M%S ", time.localtime())+penddingIndex(index)+getSuffixFromUrl(detail[1]))
            #downloadArchive(_current_url,time.strftime("%Y-%m-%d ",time.localtime())+penddingIndex(index)+getSuffixFromUrl(_current_url))
            ####### thread begin #############
            startThread(_current_url,time.strftime("%Y-%m-%d ",time.localtime())+penddingIndex(index)+getSuffixFromUrl(_current_url))
            ####### thread end #############
            index+=1
        sum = len(optionTargetList)+len(thumbnailImagesList)+len(detailTargetList)
        print('total : %d' % sum)
        processPic('图片源自%s' % shopNameList[0] if len(shopNameList)==1 else '')
    except Exception as ex:
        #print('cuttent url :%s' % _current_url)
        print('Found error:%s' % str(ex))
    finally:
        print("-----get the pic from alibaba end-----")
